﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using StockCommon;
using System.Data.OleDb;
using Utilities;
using System.Collections;
using System.Globalization;


namespace DxChartTestApp1
{
    public partial class Form1 : XtraForm
    {

        XYDiagram2D Diagram { get { return stackChartControl1.Diagram as  XYDiagram2D; } }
        //AxisRange AxisXRange { get { return Diagram != null ? Diagram..AxisX.Range : null; } }

        public Form1()
        {
            InitializeComponent();
            InitChartControl();

        }
        void InitChartControl()
        {
            stackChartControl1.AppearanceName = "Default";
            ChangeSeries(ViewType.StackedArea);
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
        //  XYDiagram diagram = (XYDiagram)stackChartControl1.Diagram;

            

            String fileName = @"D:\stockdata\bid5\1584\Stack_20120627_1584.xls";
            //String fileName = @"o:\test.xls";
            
            String sheetName = "Stack";
            DataTable table=ExcelHelper.GetDataTableFromExcelFile(fileName, ref sheetName);
            int rs = table.Rows.Count;
            DataColumnCollection dcc = table.Columns;
            foreach (DataColumn dc in dcc)
            {
                String ss = dc.ColumnName;
                System.Diagnostics.Debug.WriteLine(ss);
            }
            dataGridView1.DataSource = table;
            dataGridView1.Refresh();
            updateTableToChart(table, stackChartControl1, ViewType.StackedArea);
        }
        protected void updateTableToChart(DataTable dt, ChartControl chart, ViewType viewType)
        {
            DataColumnCollection dcc = dt.Columns;
            List<String> sellColumns = new List<string>();
            List<String> buyColumns = new List<string>();

            foreach (DataColumn dc in dcc)
            {
                String ss = dc.ColumnName;
                System.Diagnostics.Debug.WriteLine(ss + "\t" + dc.DataType.ToString());
                if (ss.StartsWith("S"))
                {
                    sellColumns.Add(ss);
                }
                if (ss.StartsWith("B"))
                {
                    buyColumns.Add(ss);
                }
            }
            Hashtable sellSeries = new Hashtable();
            foreach (String ss in sellColumns)
            {
                Series series = new Series(ss, viewType);
                sellSeries.Add(ss, series);
            }

            int rows = dt.Rows.Count;
            Series 單量series = new Series("單量", viewType);
            Series 五檔外總賣量series = new Series("五檔外總賣量series", viewType);

            int timeTip = 0;
            int pre_secondOfDay = 0;

            Boolean scaleByVolume = false;

            
            for (int i = 0; i < rows; i++)
            {
                DataRow dr = dt.Rows[i];
                int secondOfDay=ObjectColumnToInt(dr["secondOfDay"]);

                int secondDiff = secondOfDay - pre_secondOfDay;
                timeTip += secondDiff;

                String ds =(String)dr["DDETime"];
                DateTime dataTime=DateTime.Now;
                dataTime=DateTime.ParseExact (ds, "yyyy/MM/dd HH:mm:ss", null);

                pre_secondOfDay = secondOfDay;
                double 單量o = ObjectColumnToDouble(dr["單量"]);

                int step=(int)Math.Log(單量o);
                if (step < 0)
                    step = 0;
                step =(int) 單量o;
                timeTip += step;
                if (scaleByVolume == false)
                    timeTip = secondOfDay;

                單量series.Points.Add(new SeriesPoint(timeTip, new double[] { 單量o }));

                Double 五檔外總賣量v = ObjectColumnToDouble(dr["五檔外總賣量"]);
                五檔外總賣量series.Points.Add(new SeriesPoint(timeTip, new double[] { 五檔外總賣量v }));

                foreach (String k in sellColumns)
                {
                    Series series = (Series)sellSeries[k];
                   // series.Points.Add(new SeriesPoint((i + 1) * 10, new double[] { values[i] }));
                    Double v=ObjectColumnToDouble(dr[k]);
                    series.Points.Add(new SeriesPoint(timeTip, new double[] { v }));
                }
            }
            stackChartControl1.Series.Clear();
            stackChartControl1.Series.Add(單量series);
            foreach (String k in sellColumns)
            {
                Series series = (Series)sellSeries[k];
                stackChartControl1.Series.Add(series);
            }
            stackChartControl1.Series.Add(五檔外總賣量series);

            XYDiagram2D diagram = (XYDiagram2D)stackChartControl1.Diagram;
            diagram.EnableAxisXZooming = true;
            
        }
        void ChangeSeries(ViewType viewType)
        {
            double[] values = { 3.1, 2.3, 3.2, 3.9, 5.1 };
            Series series = new Series("Series", viewType);

            for (int i = 0; i < values.Length; i++)
            {
                series.Points.Add(new SeriesPoint((i + 1) * 10, new double[] { values[i] }));

            }
            stackChartControl1.Series.Clear();
            stackChartControl1.Series.Add(series);
        }
        protected int ObjectColumnToInt(Object o)
        {
            if (o is DBNull)
                return 0;
            if (o is int)
                return (int)o;
            if (o is String)
            {
                String s = (String)o;
                int r=0;
                if (int.TryParse(s, out r))
                    return r;
                return 0;
            }
            if (o is  Double)
            {
                Double d = (Double)o;
                int r = (int)d;
                return r;
            }
            System.Diagnostics.Debug.WriteLine(o.GetType().ToString());
            return 0;
        }
        protected double ObjectColumnToDouble(Object o)
        {
            if (o is DBNull)
                return 0;
            if (o is Double)
                return (Double)o;
            if (o is String)
            {
                String s = (String)o;
                Double r=  0;
                if (Double.TryParse(s, out r))
                    return r;
                return 0;
            }
            if (o is int)
            {
                int i = (int)o;
                Double r = i;
                return r;
            }
            System.Diagnostics.Debug.WriteLine(o.GetType().ToString());
            
            return 0;
        }
     
    }
}